package io.netty.codec;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * ProtoBuf 编解码器
 *
 * @author verlet
 * @date 2023-02-05 23:55:33
 */
public class Server {
    private final int port;

    public Server(int port) {
        this.port = port;
    }

    public void run() throws InterruptedException {
        // 用于接收client端连接
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        // 用于实际任务的处理
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    // 设置bossGroup的handler
                    .handler(new LoggingHandler(LogLevel.INFO))
                    // 设置workerGroup的handler
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline()
//                                    .addLast(new ProtobufDecoder(StudentPOJO.Student.getDefaultInstance()))
                            .addLast("frameDecoder",
                                    new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4))
                                    .addLast(new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()))
                                    .addLast(new ServerHandler());
                        }
                    })
                    // tcp 连接队列
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    // 发送缓冲大小
                    .childOption(ChannelOption.SO_SNDBUF, 32 * 1024)
                    // 接收缓冲大小
                    .childOption(ChannelOption.SO_RCVBUF, 32 * 1024)
                    // 保持连接
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            // 绑定端口，进行监听
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();

            // 等待连接关闭
            channelFuture.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new Server(5201).run();
    }
}
